﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using DACU.Player;
using DACU.Tools;
using Un4seen.Bass;
using MediaPlayer = DACU.Player.MediaPlayer;

namespace DACU.UserControls.Player
{
	/// <summary>
	/// Логика взаимодействия для PlayerNavigation.xaml
	/// </summary>
	public partial class PlayerNavigation : INotifyPropertyChanged
	{
		public event PropertyChangedEventHandler PropertyChanged;
		public event EventHandler PlayPauseTrigered;
		public event EventHandler StopTrigered;
		public event EventHandler PrevTrigered;
		public event EventHandler NextTrigered;
		public event ChangeVolumeEventHandler VolumeChanged;
		public event ChangedSecondsPosEventHandler PositionChanged;

		private bool _isDraging;
		//private bool _isClick;

		public float Volume
		{
			get { return VolumePopup1.Volume; }
			set { VolumePopup1.Volume = value; }
		}

		public double TrackLength
		{
			get { return sPosition.Maximum; }
			set
			{
				sPosition.Dispatcher.Invoke(new Action(() => sPosition.Maximum = value));
				ChangeProperty("TrackLength");
			}
		}

		private double _trackPosition;
		public double TrackPosition
		{
			get { return _trackPosition; }
			set
			{
				_trackPosition = value;
				ChangeProperty("TrackPosition");
			}
		}

		private string _trackArtist;
		public string TrackArtist
		{
			get { return _trackArtist; }
			set
			{
				_trackArtist = value;
				ChangeProperty("TrackArtist");
			}
		}

		private string _trackTitle;
		public string TrackTitle
		{
			get { return _trackTitle; }
			set
			{
				_trackTitle = value;
				ChangeProperty("TrackTitle");
			}
		}

		public bool IsPositionEnabled
		{
			get {return sPosition.IsEnabled; }
			set { sPosition.Dispatcher.Invoke(new Action(() => sPosition.IsEnabled = value)); }
		}

		public Window OwnerWindow { get; set; }

		public PlayerNavigation()
		{
			Initialize();
		}

		public PlayerNavigation(Window owner)
		{
			OwnerWindow = owner;
			Initialize();
		}

		void Initialize()
		{
			InitializeComponent();
			DataContext = this;
		}

		void ChangeProperty(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		public void MpActivityChanged(object sender,EventArgs e)
		{
			iPlayPause.Dispatcher.Invoke(new Action(() =>
			                                        	{
															var bi = new BitmapImage();
															bi.BeginInit();
															bi.UriSource = new Uri(
																((MediaPlayer)sender).Activity == BASSActive.BASS_ACTIVE_PLAYING ?
																	"/DACU;component/icons/Pause.png" :
																	"/DACU;component/icons/Play.png",
																UriKind.RelativeOrAbsolute);
															bi.EndInit();
			                                        		iPlayPause.Source = bi;
			                                        	}));
		}

		public void MpChangedPost(double pos)
		{
			sPosition.Dispatcher.Invoke(new Action(
											delegate
											{
												if (!_isDraging)
												{
													//if (!_isClick)
													//{
													sPosition.Value = TrackPosition = pos;
													//}
													//else
													//{
													//    _isClick = false;
													//}
												}
											}
											));
		}

		public void ChangeDownloadProgress(float percent)
		{
			pbDownload.Dispatcher.Invoke(new Action(
				delegate
					{
						pbDownload.Value = percent;
					}
			                             	));
		}

		private void bPlayPause_Click(object sender, RoutedEventArgs e)
		{
			if (PlayPauseTrigered != null)
				PlayPauseTrigered(sender, e);
			Keyboard.ClearFocus();
		}

		private void bStop_Click(object sender, RoutedEventArgs e)
		{
			if (StopTrigered != null)
				StopTrigered(sender, e);
			Keyboard.ClearFocus();
		}

		private void VolumePopup_VolumeChanged(float persent)
		{
			if (VolumeChanged != null)
				VolumeChanged(persent);
		}

		private void bPrev_Click(object sender, RoutedEventArgs e)
		{
			if (PrevTrigered != null)
				PrevTrigered(sender, e);
			Keyboard.ClearFocus();
		}

		private void bNext_Click(object sender, RoutedEventArgs e)
		{
			if (NextTrigered != null)
				NextTrigered(sender, e);
			Keyboard.ClearFocus();
		}

		private void sPosition_DragCompleted(object sender, DragCompletedEventArgs e)
		{
			_isDraging = false;
			if (PositionChanged != null)
				PositionChanged((sender as Slider).Value);
		}

		private void sPosition_DragStarted(object sender, DragStartedEventArgs e)
		{
			_isDraging = true;
		}

		//private void sPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		//{
		//    if (_isClick)
		//    {
		//        _isClick = false;
		//        if (PositionChanged != null)
		//        {
		//            PositionChanged(e.NewValue);
		//        }
		//    }
		//}

		//private void sPosition_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		//{
		//    _isClick = true;
		//}
	}
}
